{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import asyncio\n",
    "\n",
    "def callback(sleep_times, loop):\n",
    "    print(\"success time {}\".format(loop.time()))\n",
    "def stoploop(loop):\n",
    "    loop.stop()\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    loop = asyncio.get_event_loop()\n",
    "    \n",
    "    ## 根据第一个参数作为优先级，决定执行顺序\n",
    "    loop.call_later(1, callback, 1)\n",
    "    loop.call_later(2, callback, 1)\n",
    "    loop.call_later(3, callback, 1)\n",
    "    \n",
    "    ## 指定当前时钟时间的偏移时间开始执行\n",
    "#     now = loop.time()\n",
    "#     loop.call_at(now+2, callback, 2, loop)\n",
    "#     loop.call_at(now+1, callback, 1, loop)\n",
    "#     loop.call_at(now+3, callback, 3, loop)\n",
    "    \n",
    "    ## 立刻执行stoploop函数，传递loop作为参数来停止执行\n",
    "    # loop.call_soon(stoploop, loop)\n",
    "    ## 立刻执行\n",
    "#     loop.call_soon(callback, 4, loop)\n",
    "\n",
    "    ## call_soon_threadsafe() 线程安全，用法和call_soon一样\n",
    "    loop.call_soon_threadsafe(callback, 5, loop)\n",
    "    loop.run_forever()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
